package com.graduation.check.mapper;

import com.graduation.check.entity.ExtractionStats;
import com.graduation.check.entity.PaperExtractionTask;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface PaperExtractionTaskMapper {

    @Insert("INSERT INTO paper_extraction_task (student_id, file_id, file_name, extract_options, status, " +
            "extract_result, create_time, update_time) " +
            "VALUES (#{studentId}, #{fileId}, #{fileName}, #{extractOptions}, #{status}, " +
            "#{extractResult}, #{createTime}, #{updateTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insert(PaperExtractionTask task);

    @Select("SELECT * FROM paper_extraction_task WHERE student_id = #{studentId} ORDER BY create_time DESC")
    List<PaperExtractionTask> findByStudentId(Long studentId);

    @Select("SELECT * FROM paper_extraction_task WHERE student_id = #{studentId} ORDER BY create_time DESC LIMIT #{limit}")
    List<PaperExtractionTask> findRecentByStudentId(@Param("studentId") Long studentId,
                                                    @Param("limit") int limit);

    @Select("SELECT COUNT(*) as totalCount, " +
            "COALESCE(MAX(create_time), NOW()) as lastExtractTime " +
            "FROM paper_extraction_task WHERE student_id = #{studentId} AND status = 'COMPLETED'")
    ExtractionStats findStatsByStudentId(Long studentId);

    @Update("UPDATE paper_extraction_task SET status = #{status}, extract_result = #{extractResult}, " +
            "update_time = #{updateTime} WHERE id = #{id}")
    void update(PaperExtractionTask task);
}